<?php
class ToolsController extends AppController {
	public $name = 'Tools';
	public $uses=array('I18ns');

	
	function admin_index(){
		$this->render('admin_index','ajax');
	}

	function admin_addTranslateField(){
		// Get Controllers and Controller's Methods
		$data=null;
		if($this->webConfig['is_multi_lang']){
			if(!empty($this->data)){
				if(isset($this->data['Tool']['model']) && isset($this->data['Tool']['field'])){
					$model=$this->data['Tool']['model'];
					$field=$this->data['Tool']['field'];
					
					//get all id exist from i18n - record that already add translate field	 				
					$existRecord=$this->I18ns->find('list',array('conditions'=>array('model'=>$model,'field'=>$field))) ;
					//get all id record except exist id from i18n
					App::import('Model',$model);				
	 				$objModel =& new $model();
					$addRecord=$objModel->find('list',array('conditions'=>array('NOT'=>array('id'=>$existRecord)))) ;
					foreach($addRecord as $k=>$v){
						//add to i18n
						$addData=array('id'=>null,'foreign_key'=>$k,'model'=>$model,'field'=>$field,'locale'=>$this->webConfig['siteLang']);
						$this->I18ns->save($addData);
					}
					
					$this->Session->write('message',_resource('add success',true));					
				}
				else{
					$this->Session->write('error',_resource('please select model and field',true));
				}
			}
			
	 		$path = MODELS;
	 		App::uses('Folder','Utility');
	 		$folder =& new Folder($path);
	 		$models = $folder->read(); 		
	 		foreach($models[1] as $model){
	 			$className = str_replace('.php','',$model);
	 			$data[$className]=$className;
	 		}
		}
		else{
			$this->throwErrorMessage('multi languages is not avalable');
		}
 		
 	 	$this->set('data',$data);
		$this->render('admin_addTranslateField','ajax');
	}
	
	function admin_nocheck_getTranslateField($model=null){
		$data=null;
		if($model!=null){			
			App::uses($model,'Model');				
	 		$objModel =& new $model();
	 		if(isset($objModel->actsAs['Translate']) && count($objModel->actsAs['Translate'])>0){
	 			foreach($objModel->actsAs['Translate'] as $k=>$v)
	 				$data[$v]=$v;
	 		}
	 		
		} 
		$this->set('data',$data);
		$this->render('admin_getTranslateField','ajax');
	}
}
?>
